package web

import (
	"strconv"

	"gitee.com/zhyclouds/fable-scribe/internal/domain"
	"gitee.com/zhyclouds/fable-scribe/internal/service"
	"gitee.com/zhyclouds/fable-scribe/internal/web/jwt"
	"gitee.com/zhyclouds/fable-scribe/pkg/ginx"
	"gitee.com/zhyclouds/fable-scribe/pkg/logger"
	"github.com/gin-gonic/gin"
)

// ComicHandler 漫画控制器
type ComicHandler struct {
	comicService service.ComicService

	l logger.Logger
}

// NewComicHandler 创建漫画控制器实例
func NewComicHandler(comicService service.ComicService, l logger.Logger) *ComicHandler {
	return &ComicHandler{
		comicService: comicService,
		l:            l,
	}
}

func (h *ComicHandler) GenerateComic(ctx *gin.Context, req domain.ComicGenerateRequest, uc jwt.UserClaims) (ginx.Result, error) {
	// 调用服务层生成漫画
	result, err := h.comicService.GenerateComic(ctx.Request.Context(), req)
	if err != nil {
		h.l.Error("生成漫画失败",
			logger.Int64("userid", uc.Uid),
			logger.String("error", err.Error()))
		return ginx.Result{
			Code: 5,
			Msg:  "系统错误",
		}, nil
	}

	return ginx.Result{
		Msg:  "ok",
		Data: result,
	}, nil
}

func (h *ComicHandler) GenerateComicV1(ctx *gin.Context, req domain.ComicGenerateRequest, uc jwt.UserClaims) (ginx.Result, error) {
	res, err := h.comicService.GenerateComicSeries(ctx, uc.Uid, req)
	if err != nil {
		h.l.Error("生成漫画失败",
			logger.Int64("userid", uc.Uid),
			logger.String("error", err.Error()))
		return ginx.Result{
			Code: 5,
			Msg:  "系统错误",
		}, nil
	}

	return ginx.Result{
		Msg:  "ok",
		Data: res,
	}, nil
}

func (h *ComicHandler) GenerateList(ctx *gin.Context, req GenerateListReq, uc jwt.UserClaims) (ginx.Result, error) {
	if req.Offset < 0 || req.Limit > 10 || req.Limit < 0 {
		return ginx.Result{
			Code: 4,
			Msg:  "参数不合法",
		}, nil
	}

	res, err := h.comicService.GenerateList(ctx, uc.Uid, req.Offset, req.Limit)
	if err != nil {
		h.l.Error("获取作品失败",
			logger.Int64("userid", uc.Uid),
			logger.String("error", err.Error()))
		return ginx.Result{
			Code: 5,
			Msg:  "系统错误",
		}, nil
	}

	return ginx.Result{
		Msg:  "ok",
		Data: res,
	}, nil
}

func (h *ComicHandler) DeleteComic(ctx *gin.Context, uc jwt.UserClaims) (ginx.Result, error) {
	id := ctx.Query("id")
	comicId, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		return ginx.Result{
			Code: 4,
			Msg:  "参数不合法",
		}, nil
	}

	err = h.comicService.DeleteComicById(ctx, comicId, uc.Uid)
	if err == service.ErrMongoRecodeNotFound {
		return ginx.Result{
			Code: 4,
			Msg:  "作品不存在",
		}, nil
	}

	if err != nil {
		h.l.Error("删除作品失败", logger.Int64("comic_id", comicId))
		return ginx.Result{
			Code: 5,
			Msg:  "系统错误",
		}, nil
	}

	return ginx.Result{
		Msg: "ok",
	}, nil
}

// RegisterRoutes 注册路由
func (h *ComicHandler) RegisterRoutes(server *gin.Engine) {
	generateGroup := server.Group("/generate")
	{
		generateGroup.POST("/draw", ginx.WrapBodyAndToken[domain.ComicGenerateRequest, jwt.UserClaims](h.GenerateComicV1))
		generateGroup.GET("/list", ginx.WrapBodyAndToken[GenerateListReq, jwt.UserClaims](h.GenerateList))
		generateGroup.POST("/delete", ginx.WrapToken[jwt.UserClaims](h.DeleteComic))
	}
}
